package top.juc.plugin.idea.mavenversionuploadx.util;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

public class VersionUtil {

    public static String upgradeVersion(String version)
    {
        String upgradeVersion;
        if (StringUtils.isEmpty(version)){
            version = "1.0.0";
        }

        List<String> versionControllerArr = Arrays.asList(StringUtils.split(version, "-"));
        String suffix = "";
        if(versionControllerArr.size() > 1){
            StringBuilder builder = new StringBuilder("-");
            for (int i = 1; i < versionControllerArr.size(); i++) {
                builder.append(versionControllerArr.get(i)).append("-");
            }
            builder.deleteCharAt(builder.length() - 1);
            suffix = builder.toString();
        }


        //将版本号拆解成整数数组
        List<String> strArr = Arrays.asList(StringUtils.split(versionControllerArr.get(0), "."));
        Integer[] ints = new Integer[strArr.size()];
        for(int i=0 ;i<strArr.size(); i++) {
            ints[i] = Integer.parseInt(strArr.get(i));
        }
        //递归调用
        upgradeVersion(ints, ints.length - 1);

        //数组转字符串
        upgradeVersion = StringUtils.join(ints, ".");
        upgradeVersion = upgradeVersion + suffix;

        return upgradeVersion;
    }
    private static void upgradeVersion(Integer[] ints, int index)
    {
        if (index == 0) {
            ints[0] = ints[0] + 1;
        } else {
            int value = ints[index] + 1;
            if (value < 100) {
                ints[index] = value;
            } else {
                ints[index] = 0;
                upgradeVersion(ints, index - 1);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("version:1.0.0-SNAPSHOT|"+upgradeVersion("1.0.0-SNAPSHOT"));
        System.out.println("version:1.0.0.99-RELEASE|"+upgradeVersion("1.0.0.99-RELEASE"));
        System.out.println("version:1.0.1.99|"+upgradeVersion("1.0.1.99"));
        System.out.println("version:1.0.99.99|"+upgradeVersion("1.0.99.99"));
        System.out.println("version:1.99.99.99|"+upgradeVersion("1.99.99.99"));
    }
}
